package com.qk.interceptor;

import com.qk.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri=request.getRequestURI();
        if(uri.contains("/login")){
            log.info("登录请求，放行");
            return true;
        }

        String jwt=request.getHeader("token");

        if(jwt==null||"".equals(jwt)){
            log.info("令牌不存在");
            response.setStatus(401);
            return false;
        }
        try{
            JwtUtils.parseToken(jwt);
        }catch (Exception e) {
            log.info("令牌不正确");
            response.setStatus(401);
            return  false;
        }

        return true;
    }
}
